package com.example.administrator.u17.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.u17.activity.CartanActivity;
import com.example.administrator.u17.activity.MoreActivity;
import com.example.administrator.u17.activity.TwoPicActivity;
import com.example.administrator.u17.activity.UpdateActivity;
import com.example.administrator.u17.activity.WebViewActivity;
import com.example.administrator.u17.activity.ZhangJieActivity;
import com.example.administrator.u17.model.TuiJianComicLists;
import com.example.administrator.u17.utils.IdUtils;
import com.example.administrator.u17.R;
import com.squareup.picasso.Picasso;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * Created by Administrator on 2016/11/14.
 */

public class TuiJianAdapter extends BaseAdapter{
    private Context context;
    private List<TuiJianComicLists> list;

    public TuiJianAdapter(Context context, List<TuiJianComicLists> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public int getViewTypeCount() {
        return 6;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 2 || position == 8) {
            return 0;
        } else if (position == 1 || position == 3 || position == 4 || position == 7) {
            return 1;
        } else if (position == 5) {
            return 2;
        } else if (position == 6) {
            return 3;
        } else if (position == list.size() - 1) {
            return 4;
        }
        return 5;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        TuijianViewHolerTwo two;
        TuijianViewHolderThree three;
        TuijianViewHolderFour four;
        TuijianViewHolderFor aFor;
        TuijianViewHolderFive five;
        TuijianViewHolderSix six;
        List<Integer> ids;
        final TuiJianComicLists comicLists = list.get(position);
        int type = getItemViewType(position);
        switch (type) {
            case 0:
                if (convertView == null) {
                    convertView = View.inflate(context, R.layout.item_tuijian_two, null);
                    two = new TuijianViewHolerTwo();
                    ids = IdUtils.getIds("iv_item_tuijian_two_");
                    two.ivs = new ImageView[ids.size()];
                    for (int i = 0; i < ids.size(); i++) {
                        two.ivs[i] = (ImageView) convertView.findViewById(ids.get(i));
                    }
                    ids = IdUtils.getIds("tv_item_tuijian_two_");
                    two.tvs = new TextView[ids.size()];
                    for (int i = 0; i < ids.size(); i++) {
                        two.tvs[i] = (TextView) convertView.findViewById(ids.get(i));
                    }
                    convertView.setTag(two);
                } else {
                    two = (TuijianViewHolerTwo) convertView.getTag();
                }
                Picasso.with(context).load(comicLists.titleIconUrl).into(two.ivs[0]);
                for (int i = 0; i < 2; i++) {
                    Picasso.with(context).load(comicLists.comics.get(i).cover).into(two.ivs[i + 1]);
                }
                two.tvs[0].setText(comicLists.itemTitle);
                for (int i = 0; i < 2; i++) {
                    two.tvs[2 * i + 3].setText(comicLists.comics.get(i).name);
                    two.tvs[2 * i + 2].setVisibility(View.GONE);
                }
                two.tvs[1].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (position==2){
                            Intent intent=new Intent(context, TwoPicActivity.class);
                            context.startActivity(intent);
                        }else if (position==8){
                            Intent intent=new Intent(context, CartanActivity.class);
                            context.startActivity(intent);
                        }
                    }
                });

                two.ivs[2].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (position==8){
                            Intent intent = new Intent(context, WebViewActivity.class);
                            intent.putExtra("url","http://m.u17.com/cartoon/1.html ");
                            context.startActivity(intent);
                        }
                    }
                });



                break;
            case 1:
                if (convertView == null) {
                    convertView = View.inflate(context, R.layout.item_tuijian_three, null);
                    three = new TuijianViewHolderThree();
                    ids = IdUtils.getIds("iv_item_tuijian_three_");
                    three.ivs = new ImageView[ids.size()];
                    for (int i = 0; i < ids.size(); i++) {
                        three.ivs[i] = (ImageView) convertView.findViewById(ids.get(i));
                    }
                    ids = IdUtils.getIds("tv_item_tuijian_three_");
                    three.tvs = new TextView[ids.size()];
                    for (int i = 0; i < ids.size(); i++) {
                        three.tvs[i] = (TextView) convertView.findViewById(ids.get(i));
                    }
                    convertView.setTag(three);
                } else {
                    three = (TuijianViewHolderThree) convertView.getTag();
                }

                Picasso.with(context).load(comicLists.titleIconUrl).into(three.ivs[0]);
                for (int i = 0; i < 3; i++) {
                    Picasso.with(context).load(comicLists.comics.get(i).cover).into(three.ivs[i + 1]);
                    final int finalI = i;
                    three.ivs[i+1].setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent(context,ZhangJieActivity.class);
                            intent.putExtra("id",comicLists.comics.get(finalI).comicId+"");
                            context.startActivity(intent);
                        }
                    });
                }

                three.tvs[0].setText(comicLists.itemTitle);
                for (int i = 0; i < 3; i++) {
                    three.tvs[2 * i + 3].setText(comicLists.comics.get(i).name);
                    three.tvs[2 * i + 2].setVisibility(View.GONE);
                }
                three.tvs[1].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (position==1) {
                            Intent intent = new Intent(context, MoreActivity.class);
                            intent.putExtra("title", comicLists.itemTitle);
                            intent.putExtra("argValue", "9");
                            intent.putExtra("argName", "topic");
                            context.startActivity(intent);
                        }else if (position==3){
                            Intent intent = new Intent(context, MoreActivity.class);
                            intent.putExtra("title", comicLists.itemTitle);
                            intent.putExtra("argValue", "0");
                            intent.putExtra("argName", "sort");
                            context.startActivity(intent);
                        }else if (position==4){
                            Intent intent = new Intent(context, MoreActivity.class);
                            intent.putExtra("title", comicLists.itemTitle);
                            intent.putExtra("argValue", "12");
                            intent.putExtra("argName", "topic");
                            context.startActivity(intent);
                        }
                    }
                });
                break;
            case 2:
                if (convertView == null) {
                    convertView = View.inflate(context, R.layout.item_tuijian_four_nomal, null);
                    four = new TuijianViewHolderFour();
                    ids = IdUtils.getIds("iv_item_tuijian_four_");
                    four.ivs = new ImageView[ids.size()];
                    for (int i = 0; i < ids.size(); i++) {
                        four.ivs[i] = (ImageView) convertView.findViewById(ids.get(i));
                    }
                    ids = IdUtils.getIds("tv_item_tuijian_four_");
                    four.tvs = new TextView[ids.size()];
                    for (int i = 0; i < ids.size(); i++) {
                        four.tvs[i] = (TextView) convertView.findViewById(ids.get(i));
                    }
                    convertView.setTag(four);
                } else {
                    four = (TuijianViewHolderFour) convertView.getTag();
                }
                Picasso.with(context).load(comicLists.titleIconUrl).into(four.ivs[0]);
                for (int i = 0; i < 4; i++) {
                    Picasso.with(context).load(comicLists.comics.get(i).cover).into(four.ivs[i + 1]);
                    final int finalI = i;
                    four.ivs[i+1].setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent(context,ZhangJieActivity.class);
                            intent.putExtra("id",comicLists.comics.get(finalI).comicId+"");
                            context.startActivity(intent);
                        }
                    });
                }
                four.tvs[0].setText(comicLists.itemTitle);
                four.tvs[1].setVisibility(View.GONE);
                four.tvs[2].setText(comicLists.comics.get(0).name);
                four.tvs[4].setVisibility(View.GONE);
                four.tvs[5].setText(comicLists.comics.get(1).name);
                four.tvs[6].setVisibility(View.GONE);
                four.tvs[7].setText(comicLists.comics.get(2).name);
                four.tvs[8].setVisibility(View.GONE);
                four.tvs[9].setText(comicLists.comics.get(3).name);
                four.tvs[3].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        context.startActivity(new Intent(context, UpdateActivity.class));
                    }
                });
                break;
            case 3:
                if (convertView == null) {
                    convertView = View.inflate(context, R.layout.item_tuijian_four, null);
                    aFor = new TuijianViewHolderFor();
                    ids = IdUtils.getIds("iv_item_tuijian_for_");
                    aFor.ivs = new ImageView[ids.size()];
                    for (int i = 0; i < ids.size(); i++) {
                        aFor.ivs[i] = (ImageView) convertView.findViewById(ids.get(i));
                    }
                    ids = IdUtils.getIds("tv_item_tuijian_for_");
                    Log.e("TAG", "getView: " + ids.size());
                    aFor.tvs = new TextView[ids.size()];
                    for (int i = 0; i < ids.size(); i++) {
                        aFor.tvs[i] = (TextView) convertView.findViewById(ids.get(i));
                    }
                    convertView.setTag(aFor);
                } else {
                    aFor = (TuijianViewHolderFor) convertView.getTag();
                }
                Picasso.with(context).load(comicLists.titleIconUrl).into(aFor.ivs[1]);
                Picasso.with(context).load(comicLists.comics.get(0).cover).into(aFor.ivs[0]);
                Picasso.with(context).load(comicLists.comics.get(1).cover).into(aFor.ivs[2]);
                Picasso.with(context).load(comicLists.comics.get(2).cover).into(aFor.ivs[3]);
                Picasso.with(context).load(comicLists.comics.get(3).cover).into(aFor.ivs[4]);

                aFor.ivs[0].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(context,ZhangJieActivity.class);
                        intent.putExtra("id",comicLists.comics.get(0).comicId+"");
                        context.startActivity(intent);
                    }
                });
                aFor.ivs[2].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(context,ZhangJieActivity.class);
                        intent.putExtra("id",comicLists.comics.get(1).comicId+"");
                        context.startActivity(intent);
                    }
                });
                aFor.ivs[3].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(context,ZhangJieActivity.class);
                        intent.putExtra("id",comicLists.comics.get(2).comicId+"");
                        context.startActivity(intent);
                    }
                });
                aFor.ivs[4].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(context,ZhangJieActivity.class);
                        intent.putExtra("id",comicLists.comics.get(3).comicId+"");
                        context.startActivity(intent);
                    }
                });

                aFor.tvs[0].setText(comicLists.comics.get(0).author_name);
                aFor.tvs[1].setText(comicLists.itemTitle);
                aFor.tvs[3].setVisibility(View.GONE);
                aFor.tvs[4].setText(comicLists.comics.get(1).name);
                aFor.tvs[5].setVisibility(View.GONE);
                aFor.tvs[6].setText(comicLists.comics.get(2).name);
                aFor.tvs[7].setText(comicLists.comics.get(0).name);
                aFor.tvs[8].setVisibility(View.GONE);
                aFor.tvs[9].setText(comicLists.comics.get(3).name);
                aFor.tvs[10].setText(comicLists.comics.get(0).description);
                break;
            case 4:
                if (convertView == null) {
                    convertView = View.inflate(context, R.layout.item_tuijian_five, null);
                    five = new TuijianViewHolderFive();
                    ids = IdUtils.getIds("iv_item_tuijian_five_");
                    five.ivs = new ImageView[ids.size()];
                    for (int i = 0; i < ids.size(); i++) {
                        five.ivs[i] = (ImageView) convertView.findViewById(ids.get(i));
                    }
                    ids = IdUtils.getIds("tv_item_tuijian_five_");
                    five.tvs = new TextView[ids.size()];
                    for (int i = 0; i < ids.size(); i++) {
                        five.tvs[i] = (TextView) convertView.findViewById(ids.get(i));
                    }
                    convertView.setTag(five);
                } else {
                    five = (TuijianViewHolderFive) convertView.getTag();
                }
                Picasso.with(context).load(comicLists.titleIconUrl).into(five.ivs[1]);
                Picasso.with(context).load(comicLists.comics.get(0).cover).into(five.ivs[0]);
                Picasso.with(context).load(comicLists.comics.get(1).cover).into(five.ivs[2]);
                Picasso.with(context).load(comicLists.comics.get(2).cover).into(five.ivs[3]);
                Picasso.with(context).load(comicLists.comics.get(3).cover).into(five.ivs[4]);
                Picasso.with(context).load(comicLists.comics.get(4).cover).into(five.ivs[5]);

                five.ivs[0].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(context,ZhangJieActivity.class);
                        intent.putExtra("id",comicLists.comics.get(0).comicId+"");
                        context.startActivity(intent);
                    }
                });
                five.ivs[2].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(context,ZhangJieActivity.class);
                        intent.putExtra("id",comicLists.comics.get(1).comicId+"");
                        context.startActivity(intent);
                    }
                });
                five.ivs[3].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(context,ZhangJieActivity.class);
                        intent.putExtra("id",comicLists.comics.get(2).comicId+"");
                        context.startActivity(intent);
                    }
                });
                five.ivs[4].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(context,ZhangJieActivity.class);
                        intent.putExtra("id",comicLists.comics.get(3).comicId+"");
                        context.startActivity(intent);
                    }
                });
                five.ivs[5].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(context,ZhangJieActivity.class);
                        intent.putExtra("id",comicLists.comics.get(4).comicId+"");
                        context.startActivity(intent);
                    }
                });

                StringBuilder builder=new StringBuilder();
                five.tvs[3].setText(comicLists.itemTitle);
                five.tvs[0].setText(comicLists.comics.get(0).author_name);
                for (int i = 0; i < comicLists.comics.get(0).tags.size(); i++) {
                    builder.append(comicLists.comics.get(0).tags.get(i)+" ");
                }
                five.tvs[1].setText(builder);
                five.tvs[2].setText(comicLists.comics.get(0).name);
                five.tvs[4].setText(comicLists.comics.get(1).author_name);
                for (int i = 0; i < comicLists.comics.get(1).tags.size(); i++) {
                    builder.append(comicLists.comics.get(1).tags.get(i)+" ");
                }
                five.tvs[5].setText(builder);
                five.tvs[6].setText(comicLists.comics.get(1).name);
                five.tvs[7].setText(comicLists.comics.get(2).author_name);
                for (int i = 0; i < comicLists.comics.get(2).tags.size(); i++) {
                    builder.append(comicLists.comics.get(2).tags.get(i)+" ");
                }
                five.tvs[8].setText(builder);
                five.tvs[9].setText(comicLists.comics.get(2).name);
                five.tvs[10].setText(comicLists.comics.get(3).author_name);
                for (int i = 0; i < comicLists.comics.get(3).tags.size(); i++) {
                    builder.append(comicLists.comics.get(3).tags.get(i)+" ");
                }
                five.tvs[11].setText(builder);
                five.tvs[12].setText(comicLists.comics.get(3).name);
                five.tvs[13].setText(comicLists.comics.get(4).author_name);
                for (int i = 0; i < comicLists.comics.get(4).tags.size(); i++) {
                    builder.append(comicLists.comics.get(4).tags.get(i)+" ");
                }
                five.tvs[14].setText(builder);
                five.tvs[15].setText(comicLists.comics.get(4).name);

                break;
            case 5:
                if (convertView == null) {
                    convertView = View.inflate(context, R.layout.item_tuijian_six, null);
                    six = new TuijianViewHolderSix();
                    ids = IdUtils.getIds("iv_item_tuijian_six_");
                    six.ivs = new ImageView[ids.size()];
                    for (int i = 0; i < ids.size(); i++) {
                        six.ivs[i] = (ImageView) convertView.findViewById(ids.get(i));
                    }
                    ids = IdUtils.getIds("tv_item_tuijian_six_");
                    six.tvs = new TextView[ids.size()];
                    for (int i = 0; i < ids.size(); i++) {
                        six.tvs[i] = (TextView) convertView.findViewById(ids.get(i));
                    }
                    convertView.setTag(six);
                } else {
                    six = (TuijianViewHolderSix) convertView.getTag();
                }

                Picasso.with(context).load(comicLists.titleIconUrl).into(six.ivs[1]);
                Picasso.with(context).load(comicLists.comics.get(4).cover).into(six.ivs[0]);
                Picasso.with(context).load(comicLists.comics.get(3).cover).into(six.ivs[2]);
                Picasso.with(context).load(comicLists.comics.get(0).cover).into(six.ivs[3]);
                Picasso.with(context).load(comicLists.comics.get(5).cover).into(six.ivs[4]);
                Picasso.with(context).load(comicLists.comics.get(2).cover).into(six.ivs[5]);
                Picasso.with(context).load(comicLists.comics.get(1).cover).into(six.ivs[6]);

                six.ivs[0].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(context,ZhangJieActivity.class);
                        intent.putExtra("id",comicLists.comics.get(4).comicId+"");
                        context.startActivity(intent);
                    }
                });
                six.ivs[2].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(context,ZhangJieActivity.class);
                        intent.putExtra("id",comicLists.comics.get(3).comicId+"");
                        context.startActivity(intent);
                    }
                });
                six.ivs[3].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(context,ZhangJieActivity.class);
                        intent.putExtra("id",comicLists.comics.get(0).comicId+"");
                        context.startActivity(intent);
                    }
                });
                six.ivs[4].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(context,ZhangJieActivity.class);
                        intent.putExtra("id",comicLists.comics.get(5).comicId+"");
                        context.startActivity(intent);
                    }
                });
                six.ivs[5].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(context,ZhangJieActivity.class);
                        intent.putExtra("id",comicLists.comics.get(2).comicId+"");
                        context.startActivity(intent);
                    }
                });
                six.ivs[6].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(context,ZhangJieActivity.class);
                        intent.putExtra("id",comicLists.comics.get(1).comicId+"");
                        context.startActivity(intent);
                    }
                });


                six.tvs[2].setText(comicLists.itemTitle);
                six.tvs[1].setText(comicLists.comics.get(4).name);
                six.tvs[0].setVisibility(View.GONE);
                six.tvs[4].setText(comicLists.comics.get(3).name);
                six.tvs[3].setVisibility(View.GONE);
                six.tvs[7].setText(comicLists.comics.get(0).name);
                six.tvs[6].setVisibility(View.GONE);
                six.tvs[9].setText(comicLists.comics.get(5).name);
                six.tvs[8].setVisibility(View.GONE);
                six.tvs[11].setText(comicLists.comics.get(2).name);
                six.tvs[10].setVisibility(View.GONE);
                six.tvs[13].setText(comicLists.comics.get(1).name);
                six.tvs[12].setVisibility(View.GONE);
                six.tvs[5].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(context,MoreActivity.class);
                        intent.putExtra("title",comicLists.itemTitle);
                        intent.putExtra("argValue","8");
                        intent.putExtra("argName","topic");
                        context.startActivity(intent);
                    }
                });
                break;
        }
        return convertView;
    }




    class TuijianViewHolerTwo {
        TextView[] tvs;
        ImageView[] ivs;
    }

    class TuijianViewHolderThree {
        TextView[] tvs;
        ImageView[] ivs;
    }

    class TuijianViewHolderFour {
        TextView[] tvs;
        ImageView[] ivs;
    }

    class TuijianViewHolderFor {
        TextView[] tvs;
        ImageView[] ivs;
    }

    class TuijianViewHolderFive {
        TextView[] tvs;
        ImageView[] ivs;
    }

    class TuijianViewHolderSix {
        TextView[] tvs;
        ImageView[] ivs;
    }
}
